package com.hq.zb.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.hq.zb.dto.CodeDto;
import com.hq.zb.exception.BizException;
import com.hq.zb.exception.BizExceptionEnum;
import com.hq.zb.mapper.CodeMapper;
import com.hq.zb.model.Code;
import com.hq.zb.model.CodeLine;
import com.hq.zb.service.ICodeLineService;
import com.hq.zb.service.ICodeService;
import com.hq.zb.vo.CodeVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 数据字典头表 服务实现类
 * </p>
 *
 * @author huanghanrui
 * @since 2022-03-09
 */
@Service
@Transactional(rollbackFor= Exception.class)
public class CodeServiceImpl extends ServiceImpl<CodeMapper, Code> implements ICodeService {

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private ICodeLineService codeLineService;

    @Autowired
    private CodeMapper codeMapper;

    @Override
    public void saveCode(CodeDto codeDto) {
        Code code = new Code();
        BeanUtil.copyProperties(codeDto, code);
        if (StringUtils.isEmpty(codeDto.getCode())) {
            throw new BizException(BizExceptionEnum.DATA_NOT_NULL, "数据字典编码不允许为空");
        }
        if (StringUtils.isEmpty(codeDto.getName())) {
            throw new BizException(BizExceptionEnum.DATA_NOT_NULL, "数据字典名称不允许为空");
        }
        Wrapper<Code> wrapper = new EntityWrapper<>();
        wrapper.eq("code", codeDto.getCode());
        wrapper.eq("delete_flag", false);
        List<Code> codeRulesHeaderList =  this.selectList(wrapper);
        codeDto.setEnableFlag(true);
        if (codeDto.getCodeId() == null) {
            if (CollectionUtil.isNotEmpty(codeRulesHeaderList)) {
                throw new BizException(BizExceptionEnum.DATA_NOT_ONLY, "数据字典编码系统中已存在");
            }
            this.insert(code);
        } else {
            this.updateById(code);
        }
        //保存行表
        List<CodeLine> codeLines = codeDto.getCodeLineList();
        Wrapper<CodeLine> codeLineWrapper = new EntityWrapper<>();
        codeLineWrapper.eq("code", codeDto.getCode());
        codeLineService.delete(codeLineWrapper);
        if (CollectionUtil.isNotEmpty(codeLines)) {
            for (CodeLine codeLineDto : codeLines) {
                codeLineDto.setCode(codeDto.getCode());
                if (StringUtils.isEmpty(codeLineDto.getValue())) {
                    throw new BizException(BizExceptionEnum.DATA_NOT_NULL, "数据字典值不允许为空");
                }
                if (StringUtils.isEmpty(codeLineDto.getMeaning())) {
                    throw new BizException(BizExceptionEnum.DATA_NOT_NULL, "数据字典值含义不允许为空");
                }
                codeLineService.insert(codeLineDto);
            }
        }
        //删除缓存
        redisTemplate.delete(codeDto.getCode());
    }

    @Override
    public void deleteCodes(List<Code> codes) {
        if (CollectionUtil.isNotEmpty(codes)) {
            for (Code code : codes) {
                code.setDeleteFlag(true);
                this.updateById(code);
                Wrapper<CodeLine> codeLineWrapper = new EntityWrapper<>();
                codeLineWrapper.eq("code", code.getCode());
                List<CodeLine> codeLines = codeLineService.selectList(codeLineWrapper);
                if (CollectionUtil.isNotEmpty(codeLines)) {
                    for (CodeLine codeLineDto : codeLines) {
                        codeLineDto.setDeleteFlag(true);
                        codeLineService.updateById(codeLineDto);
                    }
                }
            }
        }
    }

    @Override
    public Page<CodeVo> queryCode(Page<CodeVo> page, Wrapper<CodeVo> wrapper) {
        page.setRecords(codeMapper.queryCode(page, wrapper));
        return page;
    }

    @Override
    public void deleteCodeLines(List<CodeLine> codeLines) {
        if (CollectionUtil.isNotEmpty(codeLines)) {
            for (CodeLine codeLine : codeLines) {
                codeLine.setDeleteFlag(true);
                codeLineService.updateById(codeLine);
            }
        }
    }
}
